#include <project.h>
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
 * Revision History
 * Date						Author								Purpose			
 * 09-Sep-2005			CHIA Lee Chyi				Implementation of display for battery charging
 
   @doc
   @module logo.c - Logo Display functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Ui/logo.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define LOGO_C

/** /#define DEBUG_LOGO_C*/
#ifdef DEBUG_LOGO_C
#define DEBUG_95
#endif

#include <svctxo.h>
#include <aclconio.h>
#include <acldev.h>
#include <aclstr.h>
#include <aclutil.h>
#include <string.h>
#include <battery.h>
#include <power.h>
#include <svc.h>

#include <define.h>
#include <appl.h>
#include <ui.h>
#include <date.h>
#include <lct.h>
#include <tct.h>
#include <logo.h>
#include <format.h>
#include <menufunc.h>
#include <spdebug.h>
#include <codesec.h>
#include <msgfuncs.h>
#include <spamsg.h>
#include <msngr.h>
#include <menus.h>
#include <gds.h>
#ifdef MOBITEX_DEFINED
#include <mbtx.h>
#include <mbtx2.h>
#endif


VS_BOOL fDisplayLogoNow = VS_TRUE;
VS_BOOL fDisplayIdleIcons = VS_TRUE;
VS_BOOL fDisplayIdleMenu = VS_FALSE;

VS_BOOL fBackLightOn = VS_TRUE;
extern int inImamLineNumber;
extern int inGetHDTTotRecs(void); 
/* MULTI_LANG:
 * - User's VFT file names now come from LCT;
 * - internal ones are hard-coded (E.g., SPUTIL.VFT, PWRBYSP3.VFT)
 * - logo font file name will come from TCT (new field)
 */

void vdSetCurrentFont(const char* pszFontName) {
    set_font(pszFontName);
    strcpy(obDisp.szFontName, pszFontName);
}

void vdGetCurrentFont(char* pszFontName) {
    get_font(pszFontName);
}

/*********************************************************************************************
* @func  void | vdSetEntryFont |
*
*       Sets ASCII 8x21 font for data entry
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/

static DisplayObject obDOEntryTemp;

void vdSetEntryFont(VS_BOOL fForceFont) {
    if (!obDisp.blActionFontASC8x21 || fForceFont) {
        memcpy(&obDOEntryTemp, &obDisp, sizeof(DisplayObject));

        vdSetGridParams(GRID_8X21);
        vdSetCurrentFont(ENTRY_FONT);
    }
}

void vdCalculateCoord(int inType,int* inCol,int* inRow) {
    int inX = *inCol,
    inY = *inRow;

    switch (inType) {
    case GRID_4x8_TO_8x21:
        // Assign 8x21 Column based in 4x8 Column
        *inCol = ((inX * 21) / 8);
        switch (inX) {
        case 1:
            *inCol = 1;
            break;
        case 2:
            *inCol = 4;
            break;
        case 3:
            *inCol = 8;
            break;
        case 4:
            *inCol = 12;
            break;
        case 5:
            *inCol = 15;
            break;
        case 6:
            *inCol = 17;
            break;
        case 7:
            *inCol = 19;
            break;
        case 8:
            *inCol = 21;
            break;
        }
        // Assign 8x21 Row based in 4x8 Row
        switch (inY) {
        case 1:
            *inRow = 2;
            break;
        case 2:
            *inRow = 4;
            break;
        case 3:
            *inRow = 6;
            break;
        case 4:
            *inRow = 8;
            break;
        }
        break;
    }
}


void vdDisplayAt_ASC8X21(int x,int y,char* m,int o) {
    int x1,
    y1,
    x2,
    y2;
    int inRow = y,
    inCol = x;
    char szFontName[FILENAME_SIZE + 1];

    wherewin(&x1, &y1, &x2, &y2);
    get_font(szFontName);
    set_font(ASC8X21_VFT);
    vdDisplayAt(inCol, inRow, m, o);
    set_font(szFontName);
    window(x1, y1, x2, y2);
}


void vdClear_8X21_Window(int inLeft,int inTop,int inRight,int inBottom) {
    int x1,
    y1,
    x2,
    y2;
    char szFontName[FILENAME_SIZE + 1];

    wherewin(&x1, &y1, &x2, &y2);
    if (obDisp.blActionFontASC8x21) {
        window(inLeft, inTop, inRight, inBottom);
        clrscr();
    } else {
        get_font(szFontName);
        set_font(ASC8X21_VFT);
        window(inLeft, inTop, inRight, inBottom);
        clrscr();
        set_font(szFontName);
    }
    window(x1, y1, x2, y2);
}

/*********************************************************************************************
* @func  void | vdResetEntryFont |
*
*       ReSets default small font for data entry from LCT record if necessary
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/

void vdResetEntryFont(VS_BOOL fForceFont) {
    if (!obDisp.blActionFontASC8x21 || fForceFont) {
        memcpy(&obDisp, &obDOEntryTemp, sizeof(DisplayObject));

        vdSetCurrentFont(obDisp.szFontName);
    }
}

/*********************************************************************************************
* @func  void | vdSetIdleFont |
*
*       Sets ASCII 4x16 font used for display of Date/Time at idle.
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/
static DisplayObject obDOIdleTemp;

void vdSetIdleFont(VS_BOOL fForceFont) {
    if (!obDisp.blMenuFontASC4x16 || fForceFont) {
        memcpy(&obDOIdleTemp, &obDisp, sizeof(DisplayObject));

        vdSetGridParams(GRID_4X16);
        vdSetCurrentFont(IDLE_FONT);
    }
}

/*********************************************************************************************
* @func  void | vdResetIdleFont |
*
*       Sets ASCII 4x16 font used for display of Date/Time at idle.
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/

void vdResetIdleFont(VS_BOOL fForceFont) {
    if (!obDisp.blMenuFontASC4x16 || fForceFont) {
        memcpy(&obDisp, &obDOIdleTemp, sizeof(DisplayObject));

        vdSetCurrentFont(obDisp.szFontName);
    }
}

/*********************************************************************************************
* @func  void | vdSetActionFont |
*
*       Sets Action font for current selected language (stored in srLCTRec).
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/

void vdSetActionFont(void) {
    char szFontName[FILENAME_SIZE + 1];

    /* Save current status of big/small font flag */
    obDisp.blPrevBigFontOn = obDisp.blCurrBigFontOn;

    /* Set new status of big/small font flag */
    obDisp.blCurrBigFontOn = FALSE;

    vdSetGridParams(shGetActionGridType());
    strcpy(szFontName, STATIC_DATA_PATH);
    strcpy(&szFontName[strlen(szFontName)], szGetActionFontName());
    vdSetCurrentFont(szFontName);
}

/*********************************************************************************************
* @func  void | vdSetMenuFont |
*
*       Sets Menu font for current selected language (stored in srLCTRec).
*
* @parm     VOID
*
* @rdesc    VOID.
*
* @end
**********************************************************************************************/

void vdSetMenuFont(void) {
    char szFontName[FILENAME_SIZE + 1];

    /* Save current status of big/small font flag */
    obDisp.blPrevBigFontOn = obDisp.blCurrBigFontOn;

    /* Set new status of big/small font flag */
    obDisp.blCurrBigFontOn = TRUE;

    vdSetGridParams(shGetMenuGridType());
    strcpy(szFontName, STATIC_DATA_PATH);
    strcpy(&szFontName[strlen(szFontName)], szGetMenuFontName());
    vdSetCurrentFont(szFontName);
}

/*********************************************************************************************
* @func  void | vdSetGridParams |
*
*       Sets Display Object rows/lines & columns base on grid type.
*
* @parm char *  | pchBFName |
*
* @rdesc VOID.
*
* @end
**********************************************************************************************/

void vdSetGridParams(short shGridType) {
    obDisp.inCurFontType = shGridType;

    switch (shGridType) {
    case GRID_8X21:
        obDisp.inTitleLine = 1;
        obDisp.inInputLine = inImamLineNumber -1;
        obDisp.inStatusLine = inImamLineNumber;
        obDisp.inDetailLine = inImamLineNumber-2;
        obDisp.inHomeCol = 1;
        obDisp.inFirstLineCOne = inImamLineNumber/2;
        obDisp.inSecondLineCOne = inImamLineNumber/2+1;
        obDisp.inTablesInputLine = inImamLineNumber/2-1;

        /* Display object fields */
        obDisp.inTopEntryRow = 2; //inImamLineNumber/2-1;
        //            obDisp.inAmtInputLine         = 7; /* GetAmount, etc. */
        obDisp.in3200BarCol = 16;

        obDisp.inStatusLine = inImamLineNumber;
        obDisp.inCharsPerColumn = inImamLineNumber;
        obDisp.inCharsPerRow = 21;
        break;

    case GRID_4X16:
        obDisp.inTitleLine = 1;
        obDisp.inInputLine = 3;
        obDisp.inStatusLine = 4;
        obDisp.inDetailLine = 2;
        obDisp.inHomeCol = 1;
        obDisp.inFirstLineCOne = 2;
        obDisp.inSecondLineCOne = 3;
        obDisp.inTablesInputLine = 3;  // 8x21 based

        /* Display object fields */
        obDisp.inTopEntryRow = 2;
        //            obDisp.inAmtInputLine             = 3; /* GetAmount, etc. */
        obDisp.in3200BarCol = 12;

        obDisp.inStatusLine = 4;
        obDisp.inCharsPerColumn = 4;
        obDisp.inCharsPerRow = 16;
        break;

    case GRID_4X8:
        obDisp.inTitleLine = 1;
        obDisp.inInputLine = 3;
        obDisp.inStatusLine = 4;
        obDisp.inDetailLine = 2;
        obDisp.inHomeCol = 1;
        obDisp.inFirstLineCOne = 2;
        obDisp.inSecondLineCOne = 3;
        obDisp.inTablesInputLine = 3; // 8x21 based

        /* Display object fields */
        obDisp.inTopEntryRow = 2;
        //            obDisp.inAmtInputLine             = 3; /* GetAmount, etc. */
        obDisp.in3200BarCol = 6;

        obDisp.inStatusLine = 4;
        obDisp.inCharsPerColumn = 4;
        obDisp.inCharsPerRow = 8;
        break;

    default:
        /*
         * Display error and halt program
         .
         .
         .
         */
        break;
    }
}

short shGetIdleLogoFileType(char* pszIdleLogoName) 
{
    short shRetVal = VS_ERR;
	static unsigned int i=0;
	int inMaxHosts=inGetHDTTotRecs();
	inLoadHDTRec(i);
	i=i+1;
	if (i>=inMaxHosts) i=0;
    //strcpy(pszIdleLogoName, szGetIdleLogoName());
	{
		strcpy(pszIdleLogoName, "F:");
		strcat(pszIdleLogoName, szGetLGOFileName());
		strcat(pszIdleLogoName, ".VFT");
	}
	if (strlen(pszIdleLogoName)==6) strcpy(pszIdleLogoName, szGetIdleLogoName());

    strupr(pszIdleLogoName);
    if (strstr(pszIdleLogoName, ".VFT") != NULL)
        shRetVal = VFT_IDLE_LOGO;
    else if (strstr(pszIdleLogoName, ".GRA") != NULL)
        shRetVal = GRA_IDLE_LOGO;

    return shRetVal;
}

#define IDLE_LOGO_START     0
#define IDLE_LOGO_END       7

void vdPutIdleLogo(void) {
    //pdebug (("--vdPutIdleLogo--"));

    if (obDisp.vdDO_idle_logo)
        obDisp.vdDO_idle_logo();
}




int idle_logo_default(void) {
    char szIdleMessage[MESSAGE_SIZE + 1];
    int inIdleLen;

    //pdebug (("--idle_logo_default--"));

    fDisplayIdleMenu = VS_FALSE;

    MEMCLR(szIdleMessage, MESSAGE_SIZE);
    vdGetMessageFromFile(IDLE_PROMPT_MSG, szIdleMessage);
    inIdleLen = strlen(szIdleMessage);

    /* For Express config/Config.sys variables the only way to clear */
    /* a value is to send one space character.  Empty variables do */
    /* not exist */
    if (inIdleLen == 1 && szIdleMessage[0] == ' ')
        inIdleLen = 0;

    if (inIdleLen > 0) {
        vdClearLogoLine();
        /* Can't display more than 16 characters */
        szIdleMessage[16] = NULL_CH;
        gotoxy(1, obDisp.inTitleLine);
        write(STDOUT, szIdleMessage, strlen(szIdleMessage));
    } /* end if there is an idle message */
    else {
        char szIdleLogoName[FILENAME_SIZE + 1];
        char szPixBuf[(IDLE_LOGO_END - IDLE_LOGO_START) + 1];
        short shIdleLogoFileType = VFT_IDLE_LOGO;

		memset(szIdleLogoName, 0x00, sizeof(szIdleLogoName));
		shIdleLogoFileType = shGetIdleLogoFileType(szIdleLogoName);

        if (shIdleLogoFileType == VFT_IDLE_LOGO) {
            int i;
            int j = 0;
            char szSavedFontName[FILENAME_SIZE + 1];

            //pdebug (("shIdleLogoFileType == VFT_IDLE_LOGO"));

            vdGetCurrentFont(szSavedFontName);
            vdClearLogoLine();
            vdSetCurrentFont(szIdleLogoName);
            for (i = IDLE_LOGO_START,j = 0; i <= IDLE_LOGO_END; i++) {
                szPixBuf[j++] = (char) i;
            }
            szPixBuf[j] = NULL_CH;
            gotoxy(1, obDisp.inTitleLine);
            write(STDOUT, szPixBuf, j);
            vdSetCurrentFont(szSavedFontName);
        } else if (shIdleLogoFileType == GRA_IDLE_LOGO) {
            //pdebug (("shIdleLogoFileType == GRA_IDLE_LOGO"));
            fDisplayIdleMenu = VS_TRUE;
            fDisplayIdleIcons = VS_FALSE;
#ifdef  MESSENGER_DEFINED
            vdDisplayAllGraphic();
#else
            vdDisplayGraphic(szIdleLogoName, VS_TRUE, 0, 0);
#endif
        } else {
            //pdebug (("shIdleLogoFileType == UNKNOWN"));
        }
    } /* end else use standard SoftPay logo */

    return(VS_SUCCESS);
}

void vdPutCopyRightLogo(const char* pszLogoFontFileName) {
    char szPixBuf[32 + 1];
    int i = 0;
    int j = 0;
    char szSavedFontName[FILENAME_SIZE + 1];

    vdGetCurrentFont(szSavedFontName);
    vdSetCurrentFont(pszLogoFontFileName);

    j = 0;
    for (i = 0; i < 32; i++) {
        szPixBuf[j++] = (char) i;
    }
    szPixBuf[32] = NULL_CH;

    gotoxy(obDisp.inHomeCol, obDisp.inTitleLine);
	if (inImamLineNumber==16){
		write_at(szPixBuf + 0, 8, 1, 3);
		write_at(szPixBuf + 8, 8, 1, 4);
		write_at(szPixBuf + 16, 8, 1, 5);
		write_at(szPixBuf + 24, 8, 1, 6);
	} else {
		write_at(szPixBuf + 0, 8, 1, 1);
		write_at(szPixBuf + 8, 8, 1, 2);
		write_at(szPixBuf + 16, 8, 1, 3);
		write_at(szPixBuf + 24, 8, 1, 4);
	}

    vdSetCurrentFont(szSavedFontName);
    return;
}

/*-------------------------------------------------------------------------
    Function : vdDisplayCopyright
    Description :
    Parameters : void
    Returns :
    Notes :
--------------------------------------------------------------------------*/
//                            123456789012345678901
static char szCopyright[] = "Copyright (C) 1996-2001, VeriFone, Inc., All Rights Reserved";
static char szCopyright1[] = "Copyright(C)1996-2001";
static char szCopyright2[] = "VeriFone, Inc.";
static char szCopyright4[] = "All Rights Reserved.";

void vdDisplayCopyright() {
    vdSetEntryFont(VS_TRUE);
    vdDisplayStatusString(szCopyright1);
    SVC_WAIT(500);
    vdDisplayStatusString(szCopyright2);
    SVC_WAIT(500);
    //  vdDisplayStatusString (szCopyright3);
    //  SVC_WAIT (500);
    vdDisplayStatusString(szCopyright4);
    SVC_WAIT(500);
    vdResetEntryFont(VS_TRUE);
}

int inDisplayAtInit(TRANSACTION_OBJECT* pobTran) {
    /* MULTI_LANG: Change to get Logo font from the TCT, so that user can change */
	clrscr();
    vdPutCopyRightLogo(COPYRIGHT_FONT);    /* Ideally should be got from TCT (new field) */
    vdDisplayCopyright();

    return (VS_SUCCESS);
}

int inDisplayAtIdle(TRANSACTION_OBJECT* pobTran) {
#ifndef IMAM_REMOVE
    char chTimeBuff[FORMATTED_TIME_SIZE + 1];
    char chDateBuff[FORMATTED_DATE_SIZE + 1];

    // Check idle timeout
    // vdCheckIdleTimeOut(); //: Move to IMAM Idle

    /*
     *      Update Date/Time Display
     */

    vdDisplayDEMO();

    // Battery and Antenna
    vdDisplayWireless();
    vdDisplayBattery();



#ifdef MESSENGER_DEFINED
    vdDisplayMail();
#endif

    vdDisplayDockStatus(); //kv1 debug

    if (!inNoDisp) {
        if (fGetKeyboardLock())
            vdDispStrCentered(obDisp.szLockedMsg, obDisp.inTitleLine, CLR_EOL);
        else if (fRootMenu()) {
            static int inCurSS = 60;
            static VS_BOOL fDisplayLogo = VS_TRUE;
            int inSS = 0;

            vdGetTime(chTimeBuff);
            inSS = strn2int(chTimeBuff + 4, 2);

            if (((inCurSS != inSS) && (inSS % 3) == 0) || fDisplayLogoNow) {
                if (get_battery_status() == CRITICAL_LEVEL && get_dock_status() == HANDHELD_UNDOCKED) {
                    error_tone();
                    vdDelay(BEEP_DELAY);
                    error_tone();
                    vdDelay(BEEP_DELAY);
                    error_tone();
                }

                inCurSS = inSS;
                if (fDisplayLogo || fDisplayLogoNow) {
                    vdPutIdleLogo();
                    fDisplayLogoNow = fDisplayLogo = VS_FALSE;
                } else /** /if ( 0 ) // KV1 Debug - Static Idle Logo Display */ {
                    if (fDisplayIdleMenu) {
                        //pdebug (("fDisplayIdleMenu == TRUE"));
                        vdSelectMenu(ROOT_MENU_ID);
                    }
                    fDisplayIdleIcons = VS_TRUE;
                    /* Return to idle if failed */
                    vdClearLogoLine();
                    /* display date */
                    vdGetDate(chDateBuff, YYMMDD);
                    vdFormatDate(chDateBuff);
                    /* display time */
                    vdGetTime(chTimeBuff);
                    vdFormatTime(chTimeBuff);
                    chTimeBuff[5] = NULL_CH;
                    vdSetIdleFont(VS_FALSE);
                    vdDisplayAt(obDisp.inHomeCol, obDisp.inTitleLine, chDateBuff, NO_CLEAR);
                    vdDispStrAtRight(chTimeBuff, obDisp.inTitleLine, NO_CLEAR);
                    vdResetIdleFont(VS_FALSE);
                    fDisplayLogo = VS_TRUE;
                }
            }
            // Request Wireless Signal Stength
            else if ((inCurSS != inSS) && (inSS == 7 || inSS == 28 || inSS == 47)) {
                if (srGDS.fWirelessActive) {
#ifdef MOBITEX_DEFINED
                    if (obCommunication.inLinkType == MOBITEX_COMM) {
                        extern char gchFPacketFlag;

                        gchFPacketFlag = 0x00;
                        //pdebug(("Send SS FA Packet"));
                        inSendMobitexFAPacket();
                    }
#endif
                }
            }
        }
    }
#endif //IMAM_REMOVE
    return (VS_SUCCESS);
}

void vdDisplayDEMO(void) {
    if (fGetDemoMode() && fDisplayIdleIcons) {
        // DEMO in 2nd Column
        //gotoxy(obDisp.inHomeCol/** /+1*/, 2);
        gotoxy(obDisp.inHomeCol/** /+1*/, 5);//jrd
        vdDisplayFontChar(UTILITY_FONT, DEMO_CHAR1);
        //gotoxy(obDisp.inHomeCol/** /+1*/, 3);
        gotoxy(obDisp.inHomeCol/** /+1*/, 6);//jrd
        vdDisplayFontChar(UTILITY_FONT, DEMO_CHAR2);
    }
}

void vdDisplayEnvelop(void) {
    //pdebug (("--vdDisplayEnvelop--"));

    if (fDisplayIdleIcons)
        vdDisplayGraphic("F:ENVELOP.GRA", VS_FALSE, 1, 7);
}

void vdDisplayDockStatus(void) {
    if (fDisplayIdleIcons) {
        if (srGDS.fWirelessActive) {
            gotoxy(2, 4);
            vdDisplayFontChar(UTILITY_FONT, 21 + get_dock_status());
        }
    }
}

void vdDisplayWireless(void) {
    int inHomeCol;

    if (fDisplayIdleIcons) {
        if (srGDS.fWirelessActive) {
            inHomeCol = (fGetDemoMode() ? obDisp.inHomeCol + 1 : obDisp.inHomeCol);
            gotoxy(inHomeCol, 2);
            vdDisplayFontChar(UTILITY_FONT, ANTENNA_CHAR1 + srGDS.srWirelessSignalStatus.shGraphicIndex);
        }
    }
}

short shComputeGraphicIndex(int inPercent,int inLevels) {
    short shGraphicIndex = 0;
    int inGradient;

    inGradient = (100 / inLevels);

    if (inPercent > 0) {
        shGraphicIndex = 1;
        while (inPercent > (inGradient * (shGraphicIndex)) && shGraphicIndex < inLevels) {
            shGraphicIndex++;
        }
    }

    return (shGraphicIndex);
}

void vdGetBatteryStatus(void) {
    srGDS.srBatteryStatus.lnRawValue = get_battery_value(REMAININGCHARGE);
    srGDS.srBatteryStatus.shPercentage = get_battery_charge();
    srGDS.srBatteryStatus.shGraphicIndex = shComputeGraphicIndex(srGDS.srBatteryStatus.shPercentage, 4);
}

void vdDisplayBattery(void) {
    //int   inOffset;
    int inHomeCol;

    //LeeChyi - 09092005: Start
    static int counter=1;
	int charge=0;
	static VS_BOOL please=VS_TRUE;
	int status;
	char version[50+1];
	int inRow=1, inCol=3;
	static VS_BOOL fClearWarning=VS_FALSE;
	int inRadio=get_env_int("*RADIO");
	int inMaxBattery = get_env_int("#MAXBATT");

	memset(version, 0x00, sizeof(version));
   SVC_INFO_EPROM(version);
	if (strstr(version, "QB") == (char *) NULL){ 
		return;
	}

	if(inRadio == GPRS_RADIO || fGetDemoMode()){
		//Since Row 1 needs to display the GPRS signal strength
		inRow = 2;
		inCol = obDisp.inCharsPerColumn;
	}

	status=get_battery_status();
	charge=get_battery_charge();
	
    if (fDisplayIdleIcons) {
    	
    	//LeeChyi - 09092005: Comment Off - START
    	/*
        if (srGDS.fBatteryOperated) {
            vdGetBatteryStatus();
            inHomeCol = (fGetDemoMode() ? obDisp.inHomeCol + 1 : obDisp.inHomeCol);
            gotoxy(inHomeCol, 3);
            vdDisplayFontChar(UTILITY_FONT, BATTERY_CHAR1 + srGDS.srBatteryStatus.shGraphicIndex);
        }*/
       //LeeChyi - END
       
 		 	
 		if(charge <= 0 || status == -1)//-1 means no battery
		{
			/* LeeChyi - 21092005: START
				Use a flag to check whether backlight is on or off
			*/
			if (!fBackLightOn){
				set_backlight(VS_TRUE);
			}
			/* LeeChyi - END */
			
	      	vdMLDisplayFontChar(UTILITY_FONT, 34, inRow, inCol);
	      	
		}
		else if (charge >= 1)
		{
			if (get_dock_status() == HANDHELD_UNDOCKED) //Not Charging so just display battery strength
			{
				LOG_PRINTF(("NOT Charging - CHARGE: %d", charge)); //LeeChyi - Added this log to know the battery strength
				
				if ((charge >= 81) && (charge <= 110))
				{
					vdMLDisplayFontChar(UTILITY_FONT, 39, inRow, inCol);
				}
				else if ((charge >= 61) && (charge <= 80))
				{
					vdMLDisplayFontChar(UTILITY_FONT, 38, inRow, inCol);
				}
				else if ((charge >= 41) && (charge <= 60))
				{
					vdMLDisplayFontChar(UTILITY_FONT, 37, inRow, inCol);
				}
				else if ((charge >= 21) && (charge <= 40))
				{
					vdMLDisplayFontChar(UTILITY_FONT, 36, inRow, inCol);
				}
				else if ((charge >= 1) && (charge <= 20))
				{
					vdMLDisplayFontChar(UTILITY_FONT, 35, inRow, inCol);
			
					if(get_dock_status() == -1)//meaning using battery, if 0 using AC
					{
						if(please == VS_TRUE)
						{
							vdDisplayAt(5, 8, "PLEASE CHARGE", CLR_EOL);
							please=VS_FALSE;
							fClearWarning=VS_TRUE;	/* LeeChyi - 19092005: Added */
						}
						else
						{
							vdDisplayAt(5, 8, "   BATTERY    ", CLR_EOL);
							please=VS_TRUE;
							fClearWarning=VS_TRUE;	/* LeeChyi - 19092005: Added */
						}
					}
					else
						vdDisplayAt(5, 8, " ", CLR_EOL);
				}
			}
			
			else{
				/* LeeChyi - 21092005: START
					Use a flag to check whether backlight is on or off
				*/
				if (!fBackLightOn){
					set_backlight(VS_TRUE);
					fBackLightOn = VS_TRUE;
				}
				/* LeeChyi - END */
				
				//Charging
				//As long as it is not fully charged, display charging bars
				
				/* LeeChyi - 19092005: START
					This is to clear the warning message of "PLEASE CHARGE BATTERY" upon charging
				*/
				if (fClearWarning){
					vdDisplayAt(5, 8, " ", CLR_EOL);
					vdUpdateMenu();
					fClearWarning = VS_FALSE;
				}
				/* LeeChyi - END */
				
				if (charge >= inMaxBattery){
					//fully charged
					vdMLDisplayFontChar(UTILITY_FONT, 39, inRow, inCol);
				}
				else{
					if (counter == 1){
						vdMLDisplayFontChar(UTILITY_FONT, 35, inRow, inCol);
						counter ++;
					}
					else if (counter == 2){
						vdMLDisplayFontChar(UTILITY_FONT, 36, inRow, inCol);
						counter ++;
					}
					else if (counter == 3 ){
						vdMLDisplayFontChar(UTILITY_FONT, 37, inRow, inCol);
						counter ++;
					}
					else if (counter == 4){
						vdMLDisplayFontChar(UTILITY_FONT, 38, inRow, inCol);
						counter ++;
					}
					else if (counter == 5){
						vdMLDisplayFontChar(UTILITY_FONT, 39, inRow, inCol);
						counter = 1;
					}
				}
			}
        }
    }
}



void vdCheckIdleTimeOut(void) {
	
	/* LeeChyi - 21092005: Start
		Added an implementation for power-saving:
		For Vx610 GPRS, if ping messages are required to be sent, switch off backlight
		when not in use to save power. 
		But if ping messages are not required to be sent, then let the terminal go to SLEEP.
		For dialup, ping messages are not required. So just let the terminal go to SLEEP.
	*/
	
	int inPingTime = get_env_int("*PINGTIME");
	int inRadio=get_env_int("*RADIO");
	
    if (CHECK_TIMEOUT(srGDS.ulIdleTimerCount) == 0) {
        //pdebug (("Idle TIME OUT"));
        if (srGDS.fHandHeld && srGDS.fBatteryOperated && get_dock_status() == HANDHELD_UNDOCKED) {
            vdTerminalSleep();
            fDisplayIdleMenu = VS_TRUE;
        }

        /* This set of conditions is meant for Vx610 terminals */
       else if (srGDS.fBatteryOperated && get_dock_status() == HANDHELD_UNDOCKED) {
        	  
	      	if (inRadio == GPRS_RADIO && inPingTime > 0){
	       	set_backlight(VS_FALSE);
	           fBackLightOn = VS_FALSE;
	           fDisplayIdleMenu = VS_TRUE;
	     	}
	     	else{
          		vdTerminalSleep();
            	fDisplayIdleMenu = VS_TRUE;
        	}    
     	}
	/* LeeChyi - END */

        // Reset Idle Timer
        srGDS.ulIdleTimerCount = set_itimeout(NO_CLOCK_HANDLE inGetIdleTimeOut(), TM_SECONDS);
        if (shGetCurrentMenuID() != ROOT_MENU_INDEX)
            inCancelKeyResponder();
    }
    	
}

void vdTerminalSleep(void) {
    unsigned long       ulEventMap;

    do {
        ulEventMap = wait_event();
    } while (!(ulEventMap & (unsigned long) EVT_KBD) && !(ulEventMap & (unsigned long) EVT_SYSTEM));

    // Set Idle Timeout Time
    srGDS.ulIdleTimerCount = set_itimeout(NO_CLOCK_HANDLE inGetIdleTimeOut(), TM_SECONDS);

    //set_backlight (VS_TRUE);//KV1 Debug
}


extern int inScrollLeftKey;
extern int inScrollRightKey;

void vdToggleArrow() {
    int inX,
    inY;

    wherewincur(&inX, &inY); /* hold current */

    if (obDisp.chCaratOffset == uiDOWN_ARROW) {
        /* was down */
        obDisp.chCaratOffset = uiUP_ARROW;
        inScrollLeftKey = KEY_a;
        inScrollRightKey = 0;

        gotoxy(obDisp.inHomeCol, obDisp.inCharsPerColumn);
        if (obDisp.blCaratVis)
        {
	     // Rannie_B1 04262004: Fixed the bug wherein the down arrow icon is not shown
	     // if the language in use is not English
            //vdDisplayFontChar(UTILITY_FONT, obDisp.chCaratOffset)
            vdMLDisplayFontChar(UTILITY_FONT, obDisp.chCaratOffset, obDisp.inHomeCol, obDisp.inCharsPerColumn);
            // End Rannie_B1 04262004
	 }
        else
            write(STDOUT, " ", 1);
    } else {
        /* was up */
        obDisp.chCaratOffset = uiDOWN_ARROW;
        inScrollLeftKey = 0;
        inScrollRightKey = KEY_a;

        gotoxy(obDisp.inHomeCol, obDisp.inCharsPerColumn);
        if (obDisp.blCaratVis)
        {
	    // Rannie_B1 04262004: Fixed the bug wherein the down arrow icon is not shown
	    // if the language in use is not English
           //vdDisplayFontChar(UTILITY_FONT, obDisp.chCaratOffset);
           vdMLDisplayFontChar(UTILITY_FONT, obDisp.chCaratOffset, obDisp.inHomeCol, obDisp.inCharsPerColumn);
           // End Rannie_B1 04262004
	    }
        else
            write(STDOUT, " ", 1);
    }

    gotoxy(inX, inY); /* restore current */
} /* end vdToggleArrow() */

void vdDisplayFontChar(const char* pszFontName,char chCharNo) {
    char szSaveCurrentFont[FILENAME_SIZE + 1];

    vdGetCurrentFont(szSaveCurrentFont);
    vdSetCurrentFont(pszFontName);
    write(STDOUT, &chCharNo, 1);
    vdSetCurrentFont(szSaveCurrentFont);
}

void vdMLDisplayFontChar(const char* pszFontName,char chCharNo, int x, int y)
{
    char szSaveCurrentFont[FILENAME_SIZE + 1];
    char szBuffer[2];

    memset(szBuffer, 0x00, sizeof(szBuffer));
    vdGetCurrentFont(szSaveCurrentFont);
    vdSetCurrentFont(pszFontName);
    /*** write(STDOUT, &chCharNo, 1);***/

     sprintf(szBuffer, "%c", chCharNo);
     vdDisplayAt(x, y, szBuffer, NO_CLEAR);

    vdSetCurrentFont(szSaveCurrentFont);
}

/*-------------------------------------------------------------------------
    Function    :   void vdWriteSpace (void)
    Description :   Writes the SPACE character to the kcurrent cursor location.
    Parameters  :   None
    Returns     :   None
    Globals     :   None
    Notes       :   \x01\x20 the reference to the space character in the 2
                :   byte address font file
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
-------------------------------------------------------------------------*/

void vdWriteSpace(void) {
    if (get_font_mode() == 2)
        write(STDOUT, "\x01\x20", 2);
    else
        write(STDOUT, " ", 1);
}

void vdDisplayGraphic(const char* pszFilename,VS_BOOL fClrscr,int i_col,int i_row) {
    //int       i;
    char szSavedFontName[FILENAME_SIZE + 1];

    //pdebug (("--vdDisplayGraphic--"));

    if (fClrscr) {
        //pdebug (("fClrscr == TRUE"));
        clrscr();
    }

    vdGetCurrentFont(szSavedFontName);
    vdSetCurrentFont(ASC8X21_VFT);
    i_display_graphic(pszFilename, i_col, i_row);
    vdSetCurrentFont(szSavedFontName);
}

/***************************************************************************
  displays a graphic bitmap

  PARAMETERS:
  bp_graphic_name - name of the bitmap file.
  b_clr           - if TRUE clears the screen before displaying the graphic.

  RETURNS: TRUE if file is found and displayed, FALSE otherwise.
****************************************************************************/

int i_display_graphic(const char* bp_graphic_name,int i_col,int i_row) {
    unsigned char bp_data[sizeof(S_GRA)];
    S_GRA* s_data = (S_GRA*) bp_data;
    int h_file = 0,
    i_rec_number = 0;

    if ((h_file = open(bp_graphic_name, O_RDONLY)) == -1)
        return(VS_FALSE);

    while (i_rec_number <= 7) {
        seek_vlr(h_file, (long) i_rec_number, 0);
        read_vlr(h_file, (char *) bp_data, sizeof(bp_data));
        if (!i_row && !i_col)
            gotoxy(s_data->b_col, s_data->b_row);
        else
            gotoxy(i_col, i_row);
        putpixelcol((char *) s_data->bp_line, s_data->b_len);
        i_rec_number++;
    }

    close(h_file);

    return(VS_TRUE);
}

int LOGO_CODE_CHECK(char* a,char* b, short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}

#undef LOGO_C

